package com.gwd.task.controller;

import com.gwd.task.group.GroupInsert;
import com.gwd.task.group.GroupUpdate;
import com.gwd.task.model.CodeSubmitRecordDTO;
import com.gwd.task.model.PageUtils;
import com.gwd.task.model.ProductionEventsDTO;
import com.gwd.task.services.ProductionEventsServer;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhiyongming
 * @date 2020/12/9 18:29
 */
@RestController
@RequestMapping(value = "/v1/events")
public class EventsController {
    @Resource
    private ProductionEventsServer productionEventsServer;

    @GetMapping(value = "/findByPage")
    public PageUtils findByPage(@Validated ProductionEventsDTO dto, PageUtils page) {
        return productionEventsServer.findByPage(dto, page);
    }

    @PostMapping(value = "export")
    public void export(@RequestBody @Validated ProductionEventsDTO dto, HttpServletResponse response) {
        String fileName = "生产汇总-" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +".xls";
        HSSFWorkbook wb = productionEventsServer.export(dto);
        try{ //写入浏览器
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Type", "text/html;charset=utf-8");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            OutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @PostMapping(value = "/add")
    public void add(@RequestBody @Validated({GroupInsert.class}) ProductionEventsDTO dto, @RequestAttribute("userId") Long id) {
        productionEventsServer.save(dto, id);
    }

    @PutMapping(value = "/changeDevPerson/{id}")
    public void changeDevPerson(@PathVariable(name = "id")Long id, @RequestBody ProductionEventsDTO dto) {
        productionEventsServer.changeDevPerson(id, dto.getDevPerson());
    }

    @PutMapping(value = "/changeTestPerson/{id}")
    public void changeTestPerson(@PathVariable(name = "id")Long id, @RequestBody ProductionEventsDTO dto) {
        productionEventsServer.changeTestPerson(id, dto.getTestPerson());
    }

    @PutMapping(value = "/edit")
    public ProductionEventsDTO edit(@RequestBody @Validated({GroupUpdate.class}) ProductionEventsDTO dto,
                                    @RequestAttribute("userId") Long id) {
        productionEventsServer.save(dto, id);
        return productionEventsServer.findById(dto.getPeId());
    }

    @DeleteMapping(value = "/remove/{id}")
    public void remove(@PathVariable(value = "id") Long id) {
        productionEventsServer.remove(id);
    }

    @PutMapping(value = "/close/{id}")
    public void close(@PathVariable(value = "id") Long id, @RequestAttribute("userId") Long userId) {
        productionEventsServer.close(id, userId);
    }

    @PutMapping(value = "/start/{id}")
    public void start(@PathVariable(value = "id") Long id, @RequestAttribute("userId") Long userId) {
        productionEventsServer.start(id, userId);
    }

    @PutMapping(value = "/restart/{id}")
    public void restart(@PathVariable(value = "id") Long id, @RequestAttribute("userId") Long userId) {
        productionEventsServer.restart(id, userId);
    }

    @PutMapping(value = "/submit/{id}")
    public void submit(@PathVariable(value = "id") Long id, @RequestBody CodeSubmitRecordDTO csrDTO, @RequestAttribute("userId") Long userId) {
        productionEventsServer.submit(id, userId, csrDTO);
    }

    @PutMapping(value = "/test/{id}")
    public void test(@PathVariable(value = "id") Long id, @RequestAttribute("userId") Long userId) {
        productionEventsServer.test(id, userId);
    }

    @PutMapping(value = "/finish/{id}")
    public void finish(@PathVariable(value = "id") Long id, @RequestAttribute("userId") Long userId) {
        productionEventsServer.finish(id, userId);
    }

}
